/**
 * 
 */
package com.taobao.client.util;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 
 * 
 * @author hujingfei.pt@taobao.com
 *
 */
public class PopBox {
	private static DialogBox dialogBox;
	private static final HTML serverResponseLabel = new HTML();
	private static final Button closeButton = new Button("关闭");
	private static final String OPTIONS = "toolbar =yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, status=yes";

	static {
		dialogBox = new DialogBox();
		dialogBox.setAnimationEnabled(true);

		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>服务返回以下信息:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.add(closeButton);
		
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox.
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});
	}
	
	public static void openWindow(String url){
		Window.open(url, "_blank", OPTIONS);
	}
	
	public static void ok(String caption, String msg) {
		dialogBox.setText(caption);
		serverResponseLabel.removeStyleName("serverResponseLabelError");
		serverResponseLabel.setHTML(msg);
		dialogBox.center();
		closeButton.setFocus(true);
	}

	public static void error(String caption, String msg) {
		dialogBox.setText(caption);
		serverResponseLabel.addStyleName("serverResponseLabelError");
		serverResponseLabel.setHTML(msg);
		dialogBox.center();
		closeButton.setFocus(true);
	}
	
	public static void inform(String caption, String msg) {
		dialogBox.setText(caption);
		serverResponseLabel.addStyleName("serverResponseLabelError");
		serverResponseLabel.setHTML(msg);
		dialogBox.center();
		closeButton.setFocus(true);
	}
}
